package cn.ichiva.tushare.sdk.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static Logger logger = LoggerFactory.getLogger(DateUtils.class);

    public static String FORMAT_TU_SHARE = "yyyyMMdd";
    public static String FORMAT_TU_SHARE_MONTH = "yyyyMM";

    /** yyyy-MM */
    public static String FORMAT_TO_MONTH = "yyyy-MM";
    /** yyyy-MM-dd */
    public static String FORMAT_TO_DD = "yyyy-MM-dd";
    /** yyyy-MM-dd HH */
    public static String FORMAT_TO_HH = "yyyy-MM-dd HH";
    /** yyyy-MM-dd HH:mm */
    public static String FORMAT_TO_MM = "yyyy-MM-dd HH:mm";
    /** yyyy-MM-dd HH:mm:ss */
    public static String FORMAT_TO_SS = "yyyy-MM-dd HH:mm:ss";
    /**
     * 将字符串日期转换为Date
     *
     * @param format yyyy-MM-dd HH:mm:ss,yyyyMMddHHmmss....
     * @return
     */
    public static Date convertToDate(String dateStr, String format){
        try{
            if(null == dateStr || dateStr.length() == 0){
                return null;
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(dateStr);
        }catch (ParseException e){
            logger.error("convertToDate() error={}",e);
        }
        return null;
    }

    public static String dateToQuarter(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        return year + "Q" + (month / 3 + 1);
    }

    /**
     * 将Date日期转换为字符串日期格式
     * @param format yyyy-MM-dd HH:mm:ss,yyyyMMddHHmmss....
     * @return
     */
    public static String dateToString(Date dateStr, String format){
        try{
            if(dateStr==null){
                return "";
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(dateStr);
        }catch (Exception e){
            logger.error("dateToString() error={}",e);
        }
        return null;
    }

    /**
     * 当天剩余时间（毫秒）
     */
    public static long remainingTimeForDay(){
        return ChronoUnit.MILLIS.between(LocalDateTime.now(),LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0));
    }

    /**
     * 距离下一个小时数的时间（毫秒）
     * @param nHour
     * @return
     */
    public static long remainingTimeForNextHour(int nHour){
        int hour = LocalTime.now().getHour();
        int remainingHour = hour < nHour ? nHour -24 : nHour;
        return remainingTimeForDay() + remainingHour * 1000 * 60 * 60;
    }

    public static String lastTUDateString(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE,-1);
        Date lastTUDate = calendar.getTime();

        return dateToString(lastTUDate,FORMAT_TU_SHARE);
    }

    public static String lastTUMonthString(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH,-1);
        Date lastTUDate = calendar.getTime();

        return dateToString(lastTUDate,FORMAT_TU_SHARE_MONTH);
    }
}
